<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>
<%
	request.setAttribute("modelConfigPath", "cargo");
%>
<%@ include file="/WEB-INF/views/shared/master/contentHeader.jsp"%>
<style type="text/css">
	.template{display:none}
</style>
	
<t:panel cssClass="panel-flat form-detailed" customBody="true" customHead="true">
	<div class="detailed-navbar-content panel-body" style="height:420px">
		<t:form modelAttribute="cargo" action="/cargo/cargo/save" method="post" cssClass="form-horizontal">
			<div class="hide">
				 <t:hidden path="cargoId"/>
 				 <t:hidden path="deleteMark"/>	
 				 <t:hidden path="createUserName"/>
		 		 <t:hidden path="createUserId"/>
		 		 <t:hidden path="createDate"/>
		 		 <t:hidden path="shopId"/>
		 		 <t:hidden path="cargoNo"/>
		 		 <t:hidden path="cargoName"/>
		 		 <t:hidden path="describes"/>
		 		 <t:hidden path="cargoState"/>
		 		 <t:hidden path="categoryName"/>
		 		 <t:hidden path="isStockWarning"/>
		 		 <t:hidden path="lowSafetyStock"/>
		 		 <t:hidden path="heightSafetyStock"/>
		 		 <t:hidden path="isHideSalePrice"/>
		 		 <t:hidden path="isHideStock"/>
		 		 <t:hidden path="isHideSaleVolume"/>
		 		 <t:hidden path="shapeCode"/>
		 		 <t:hidden path="comment"/>
			</div>
			 <t:formgroup>
                <t:label path="unit" cssClass="col-sm-2" />
          		<t:select2 onchange="unitChange(this)" allowClear="true" placeholder="选择单位" path="unit" colClass="col-sm-10" />
            </t:formgroup>
            <t:formgroup>
                <t:label path="viceFirstUnit" cssClass="col-sm-2" />
                <t:select2 allowClear="true" placeholder="选择单位" path="viceFirstUnit" colClass="col-sm-3" />
                <t:label path="firstRatio" cssClass="col-sm-1" />
                <t:input path="firstRatio" placeholder="换算比率" type="number" colClass="col-sm-3" />
            	<div class="col-sm-1"><input id="unit" readonly="true"  class="form-control custom-input readUnit" data-control-type="input" data-ts-forcestepdivisibility="none" data-val-required="必填" data-val="true" type="text" value="${cargo.unit }" placeholder="请输入基本单位"><span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="unit"></span></div>
            </t:formgroup>
            <t:formgroup>
 
                
                <t:label path="viceSecondUnit" cssClass="col-sm-2" />
                <t:select2 allowClear="true" placeholder="选择单位" path="viceSecondUnit" colClass="col-sm-3" />
             	<t:label path="secondRatio" cssClass="col-sm-1" />
                <t:input path="secondRatio" placeholder="换算比率" type="number" colClass="col-sm-3" />
          		<div class="col-sm-1">
          			<input id="unit" readonly="true"  class="form-control custom-input readUnit" data-control-type="input" data-ts-forcestepdivisibility="none" data-val-required="必填" data-val="true" type="text" value="${cargo.unit }" placeholder="请输入基本单位">
          			<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="unit"></span>
          			</div>
            </t:formgroup>
		</t:form>
	</div>
	<t:panelFoot headingBtnCssClass="heading-btn pull-right-important">
		<t:button icon="icon-floppy-disk" onclick="addData()" laddaType="progress" cssClass="btn-info" csize="sm">新增单位</t:button>
		<t:button icon="icon-floppy-disk" onclick="saveData()" laddaType="progress" cssClass="btn-info" csize="sm">保存</t:button>
	</t:panelFoot>
</t:panel>

   

<script type="text/javascript">
$(function() {

	//注册下拉框选择展开监听事件
	$("#unit,#viceFirstUnit,#viceSecondUnit").on("select2:open", function (e) { 
		var units=[];
		var url =$.getVirtualPath()+"/cargo/unit/unitList";
		 $.ajax({
		        type: 'get',
		        dataType: "json",
		        url: url,
		        async: false,
		        success: function (result) {
		        	data=[];			
					if(!$.isNull(result)){
						data = result;
						data.forEach(function(element,index){
							units.push(element.unitName)
						})
					}
		        }
	    });
		var select=this;
		var unitOlds=[];
		var newList=[];
		$(select).find("option").each(function(){
			var option=$(this).val();
			unitOlds.push(option);
		})
		for(var i=0;i<units.length;i++){
			var newUnit=units[i]
			var isExist = false;
			for(var j=0;j<unitOlds.length;j++){
				var oldUnit=unitOlds[j];
				if(oldUnit==newUnit){
					isExist=true;
					break;
				}
			}
			if(!isExist){
				newList.push(newUnit);
		    }
		
			
		}
		if(!$.isNull(newList)){
			newList.forEach(function(ele,index){
				var newOption = new Option(ele, ele, false, false);
				
				$(select).append(newOption);
			})
			
		}
		
		console.log(unitOlds);

	});

});

function unitChange(element){
	console.log()
	$(".readUnit").val($(element).val())
	
}

function addData(){
	top.openModal("/cargo/unit/detailed?type="+"add","category_form","单位详情-添加",450,185);
}

function saveData(lBtn){
	var viceFirstUnit=$("#viceFirstUnit").val();
	var firstRatio=$("#firstRatio").val();
	var viceSecondUnit=$("#viceSecondUnit").val();
	var secondRatio=$("#secondRatio").val();
	if($.isNull(viceFirstUnit)&&!$.isNull(firstRatio)){
		$.notify("请选择对应单位1!!","warning");
		return;
	}
	if(!$.isNull(viceFirstUnit)&&$.isNull(firstRatio)){
		$.notify("请填写单位1比率!!","warning");
		return;
	}
	if($.isNull(viceSecondUnit)&&!$.isNull(secondRatio)){
		$.notify("请选择对应单位2!!","warning");
		return;
	}
	if(!$.isNull(viceSecondUnit)&&$.isNull(secondRatio)){
		$.notify("请填写单位2比率!!","warning");
		return;
	}
	
	if (!$("form:first").valid()){
		return;
	}
	lBtn.start();
	$("form:first").submit();
}	


</script>
<%@ include file="/WEB-INF/views/shared/base/scripts/jqueryui.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formtags.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formbuttons.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formpickers.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formselects.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formstyling.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/dialog.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/notifications.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/contentFooter.jsp"%>